<?php

namespace app\shop\controller\plus\agent;

use app\common\model\user\User as UserModel;
use app\shop\controller\Controller;
use app\shop\model\plus\agent\Apply as ApplyModel;
use app\supplier\model\supplier\Supplier as SupplierModel;

/**
 * 分销控制器
 */
class Apply extends Controller
{

    /**
     * 分销商申请列表
     */
    public function index()
    {
        $model = new ApplyModel;
        $apply_list = $model->getList($this->postData());
        return $this->renderSuccess('', compact('apply_list'));
    }

    protected function getRouteinfo()
    {
        parent::getRouteinfo(); // TODO: Change the autogenerated stub
    }

    /**
     * 商户列表
     * getSupplierList
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/5/11 15:15
     */
    public function getSupplierList()
    {
        $params =  $this->postData();
        $map = [];
        $map[] = ['is_delete','=',0];
//        显示所有的商户，因为有些商户添加的时候没有选择区域
//        if(isset($params['city_id']) && $params['city_id']){
//            $map[] = ['city_id','=',$params['city_id']];
//        }
        $list = SupplierModel::where($map) -> select() -> toArray();
        return $this->renderSuccess('', compact('list'));
    }

    /**
     * 审核分销商
     */
    public function editApplyStatus($apply_id)
    {
        $model = ApplyModel::detail($apply_id);
        $params = $this->postData();
        $apply_status = [20,30];

        if(!isset($params['apply_status']['value']) || !in_array($params['apply_status']['value'],$apply_status) ){
            return $this->renderError('请选择审核状态');
        }
        $params['apply_status'] = $params['apply_status']['value'];

//        if($params['apply_status'] == 20){
//            if(!isset($params['shop_supplier_id']) || ! $params['shop_supplier_id']){
//                return $this->renderError('请选择商户');
//            }
//        }

        if ($model->submit($params)) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');

    }

    /**
     * 删除分销商用户
     */
    public function delete()
    {
        $params = $this->postData();
        $apply_id = $params['apply_id'];
        $model = ApplyModel::detail($apply_id);
        if (!$model->delete()) {
            return $this->renderError('删除失败');
        }
        $update_data['is_golden'] = 0;
        $update_data['god_supplier_id'] = '';
        UserModel::where('user_id',$model['user_id'])->update($update_data);
        return $this->renderSuccess('删除成功');
    }


}